import React, { useState } from 'react'
import { Row, Col, Radio, InputNumber, Slider, Button, Checkbox, Popover, Switch, Input, Space, Dropdown, Tag, Tooltip } from 'antd'
import EditLink from '@/components/EditLink';
import UploadImage from '@/components/UploadImage';
import { backgroundObj } from '@/utils/utils';
import SelectRadioModel from '@/components/SelectRadioModel'
import { SketchPicker } from 'react-color';
import { CloseOutlined } from '@ant-design/icons'
import Indentation from '@/components/Indentation';
import SelectLink from '@/components/SelectLink';
import './index.less'
const Announcement = (props) => {
    const { selectData, data, edit } = props
    const { selectIndex } = data
    const {
        list,
        marginType,
        top,
        bottom,
        backgroundType,
        backgroundColor,
        backgroundImage,

        paddingTop,
        paddingLeft,

        borderRadius,
        radiusType,
        linkShow,
        linkType,
        linkSrc,
        styleType,
        desc,
        title,
        cover
    } = selectData
    const [visible, setVisible] = useState(false)
    const [editVisible, setEditVisible] = useState(false)
    const [shopEdit, setShopEdit] = useState(false)
    const [bgVisible, setBgVisible] = useState(false)
    const [iconImage, setIconImage] = useState(false)
    const [audioVisible, setAudioVisible] = useState(false)
    const navigationListProps = {
        title: "",
        src: "",
        linkType: "",
        linkData: {},
        linkSrc: '',
        showIphone: false
    }
    const renderTag = (item) => {
        const { linkType, linkSrc, linkData } = item
        if (linkType === 'customize') {
            return '链接|' + linkSrc
        } else {
            const { goodsName } = linkData
            return '商品|' + goodsName
        }
    }
    const items = (item) => {
        return [
            {
                label: (
                    <Button onClick={() => {
                        setShopEdit({
                            show: true,
                            details: [],

                        })
                    }} type='link'>商品</Button>
                ),
                key: '0',
            },
            {
                label: (
                    <Button onClick={() => {
                        setEditVisible({
                            show: true,
                            detail: item,

                        })
                    }} type='link'>自定义链接</Button>
                ),
                key: '1',
            },
        ]
    };
    const handleUpdate = (o) => {
        edit({
            list: data.list.map((val, index) => {
                if (index === selectIndex) {
                    return {
                        ...val,
                        ...o
                    }
                }
                return val
            })
        })
    }
    return <Row gutter={[12, 24]}>
        <Col
            span={24}>
            音频
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            组件模版
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            <Radio.Group onChange={(e) => {
                handleUpdate({
                    styleType: e.target.value
                })
            }} value={styleType}>
                <Radio.Button value={'1'}>样式一</Radio.Button>
                <Radio.Button value={'2'}>样式二</Radio.Button>
                <Radio.Button value={'3'}>样式三</Radio.Button>
            </Radio.Group>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            内容设置
        </Col>
        <Col
            span={24}>
            <Space>
                <div>音频 <span style={{ color: "#ff2929" }}>*</span> </div>
                <Button onClick={() => {
                    setAudioVisible({
                        show: true
                    })
                }} type="dashed"> 添加音频</Button>
            </Space>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            <Space>
                <div>标题：</div>
                <Input
                    showCount
                    maxLength={20}
                    onChange={(e) => {
                        handleUpdate({ title: e.target.value })
                    }} value={title}></Input>
            </Space>
        </Col>
        {styleType === '3' && <>
            <Col className='editor-content-item-label'
                span={24}>
                <Space>
                    <div>描述：</div>
                    <Input
                        onChange={(e) => {
                            handleUpdate({ desc: e.target.value })
                        }} value={desc}></Input>
                </Space>
            </Col>
            <Col span={24}>
                <div className='editor-content-item-label'>   封面图<span style={{ color: "#ff2929" }}>*</span>:</div>
            </Col>
            <Col span={24}>
                <div >

                    <div className='editor-set-color' onClick={(e) => {
                        setBgVisible({
                            show: true,
                            type: 'cover'
                        })
                    }}>
                        {cover ? <div style={backgroundObj(cover)} className='editor-set-uploadBlock'>
                        </div> : <div className='editor-set-uploadBlock'>
                            <div className='editor-set-upload'>
                                <i className='icon-jia'></i>
                            </div>
                        </div>}
                    </div></div>
            </Col>

        </>}
        <Col className='editor-content-item-label'
            span={24}>
            <Space>
                <div>跳转链接</div>
                <div style={{ color: '#333333' }}>{linkShow ? '显示' : '隐藏'}</div>
                <div><Switch onChange={(e) => {
                    handleUpdate({
                        linkShow: e,
                        linkType: '',
                        linkSrc: ""
                    })
                }} checked={linkShow}></Switch></div>
            </Space>
        </Col>
        {linkShow && <Col className='editor-content-item-label'
            span={24}>
            <div>
                <Space>
                    <div>
                        跳转链接:
                    </div>
                    <SelectLink handleUpdate={(e) => {
                        handleUpdate({
                            ...e
                        })
                    }} data={selectData}></SelectLink>

                </Space>

            </div>
        </Col>}
        <Col className='editor-content-item-label'
            span={24}>
            内容样式
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            背景
        </Col>
        <Col span={24}>
            <Radio.Group onChange={(e) => {
                handleUpdate({ backgroundType: e.target.value, backgroundColor:"#ffffff",backgroundImage: '' })
            }} value={backgroundType}>
                <Radio value={'color'}>颜色</Radio>
                <Radio value={'image'}>图片</Radio>
            </Radio.Group>
        </Col>
        {backgroundType === 'color' ? <Col span={24}>
            <div className='editor-set-color'>
                <div className='design-editor-content-item-label'>
                    自定义颜色
                </div>
                <div className='design-editor-link'>
                    <Space>
                        {/* <SketchPicker color={backgroundColor}
                            onChangeComplete={(e) => {
                                edit({
                                    backgroundColor: e
                                })

                            }}></SketchPicker> */}
                        <Button type='link'>重置</Button>
                        <Popover content={<SketchPicker
                            color={backgroundColor}
                            onChange={(e) => {
                                handleUpdate({
                                    backgroundColor: e.hex
                                })

                            }}></SketchPicker>} trigger={['click']} placement={'bottom'}>
                            <div className='design-color-set'>
                                <div style={{ background: backgroundColor }} className='design-color-block '></div>
                            </div>
                        </Popover>

                    </Space>
                </div>
            </div>
        </Col> : backgroundType === 'image' ? <Col span={24}>
            <div className='editor-set-color' onClick={(e) => {
                setBgVisible({
                    show: true,
                })
            }}>
                {backgroundImage ? <div style={backgroundObj(backgroundImage)} className='editor-set-uploadBlock'>
                </div> : <div className='editor-set-uploadBlock'>
                    <div className='editor-set-upload'>
                        <i className='icon-jia'></i>
                    </div>
                </div>}
            </div>
        </Col> : ""}
        <Col className='editor-content-item-label'
            span={24}>
            边角样式:
            <Radio.Group onChange={(e) => {
                handleUpdate({
                    radiusType: e.target.value
                })
            }} value={radiusType}>
                <Radio value={'rightAngle'}>直角</Radio>
                <Radio value={'roundedCorners'}>圆角</Radio>
            </Radio.Group>
        </Col>
        {radiusType === 'roundedCorners' && <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>圆角半径</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={16}
                        onChange={(e) => handleUpdate({ borderRadius: e })}
                        value={borderRadius}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={16} onChange={(e) => {
                        handleUpdate({ borderRadius: e })
                    }} value={borderRadius}></InputNumber></Col>
            </Row>
        </Col>}
        <Col className='editor-content-item-label'
            span={24}>
            组件样式
        </Col>
        <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>左右内边距</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ paddingLeft: e })}
                        value={paddingLeft}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ paddingLeft: e })
                    }} value={paddingLeft}></InputNumber></Col>
            </Row>
        </Col>
        <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>上下内边距</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ paddingTop: e })}
                        value={paddingTop}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ paddingTop: e })
                    }} value={paddingTop}></InputNumber></Col>
            </Row>
        </Col>
        <Col className='editor-content-item-label'
            span={24}>
            外边距：
            <Radio.Group onChange={(e) => {
                handleUpdate({
                    top: 12,
                    bottom: 0,
                    marginType: e.target.value
                })
            }} value={marginType}>
                <Radio value={'detault'}>默认</Radio>
                <Radio value={'customize'}>自定义</Radio>
            </Radio.Group>
        </Col>
        {marginType === 'customize' && <Col
            span={24}>
            <Row gutter={[12, 12]}>
                <Col span={6}>上</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ top: e })}
                        value={top}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ top: e })
                    }} value={top}></InputNumber></Col>
            </Row>
            <Row gutter={[12, 12]}>
                <Col span={6}>下</Col>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={32}
                        onChange={(e) => handleUpdate({ bottom: e })}
                        value={bottom}
                    />
                </Col>
                <Col span={6}><InputNumber min={0}
                    max={32} onChange={(e) => {
                        handleUpdate({ bottom: e })
                    }} value={bottom}></InputNumber></Col>
            </Row>
        </Col>}
        <UploadImage handleImage={(e) => {
            if (e) {
                const { src } = e
                const { type, index } = visible
                if (type === 'edit') {
                    handleUpdate({
                        list: list.map((item, o) => {
                            if (index === o) {
                                return {
                                    ...item,
                                    src
                                }
                            }
                            else {
                                return item
                            }
                        })
                    })
                } else {
                    handleUpdate({
                        list: [...list, {
                            ...navigationListProps,
                            src
                        }]
                    })
                }

            }
        }} visible={visible} onClose={() => { setVisible(false) }}></UploadImage>
        <UploadImage contentType='voice' handleImage={(e) => {
            if (e) {
                const { src, materialName } = e
                handleUpdate({
                    src: src,
                    title: materialName
                })
            }
        }} visible={audioVisible} onClose={() => { setAudioVisible(false) }}></UploadImage>

        <UploadImage handleImage={(e) => {
            const { type = 'bg' } = bgVisible
            if (e) {
                const { src } = e
                if (type === 'bg') {
                    handleUpdate({
                        backgroundImage: src
                    })
                }

                else {
                    handleUpdate({
                        cover: src
                    })
                }

            }
        }} visible={bgVisible} onClose={() => { setBgVisible(false) }}></UploadImage>
        <UploadImage handleImage={(e) => {
            if (e) {
                const { src } = e
                handleUpdate({
                    iconSrc: src
                })
            }
        }} visible={iconImage} onClose={() => { setIconImage(false) }}></UploadImage>
        <EditLink handleCallback={(e) => {
            handleUpdate({
                linkType: 'customize',
                ...e
            })
        }} visible={editVisible} onClose={() => { setEditVisible(false) }}></EditLink>
        <SelectRadioModel onOk={(e) => {
            if (e[0]) {
                handleUpdate({
                    linkType: 'shop',
                    linkData: e[0]
                })
            }
        }} visible={shopEdit} onClose={() => setShopEdit(false)}></SelectRadioModel>
    </Row>
}
export default Announcement
